package com.yixia.web.controller.system;

import com.yixia.common.core.domain.entity.SysDictType;
import com.yixia.common.enums.BusinessType;
import com.yixia.common.core.controller.BaseController;
import com.yixia.common.core.domain.AjaxResult;
import com.yixia.common.annotation.Log;
import com.yixia.common.utils.page.PageBean;
import com.yixia.common.utils.page.PageUtils;
import com.yixia.system.service.system.SysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: yimeng
 */


@Slf4j
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private SysDictTypeService dictTypeService;

    /**
     * 字典类型查询
     * @param dictType 分页条件
     */
    @GetMapping("/list")
    @PreAuthorize("@yixia.hasPermi('system:dict:list')")
    public AjaxResult list(SysDictType dictType){
        /*里面都获取请求参数中的元素并将其封装为page对象*/
        PageBean pageBean = PageUtils.startPageBean();
        /*进行分页查询*/
        return dictTypeService.selectDictDataPage(pageBean, dictType);/*返回的是page的数据集合*/
    }

    /**
     * 根据id来查询dictType信息
     *
     * @param dictId
     * @return
     */
    @GetMapping("/info/{dictId}")
    @PreAuthorize("@yixia.hasPermi('system:dict:query')")
    public AjaxResult getInfo(@PathVariable Long dictId)
    {
        SysDictType sysDictType = dictTypeService.selectDictTypeById(dictId);
        return success(sysDictType);
    }

    /**
     * 添加字典类型
     */
    @PostMapping("/add")
    @PreAuthorize("@yixia.hasPermi('system:dict:add')")
    @Log(title = "字典类型--添加字典类型", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody SysDictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.insertDictType(dict));
    }


    /**
     * 修改字典类型
     */
    @PutMapping("/edit")
    @PreAuthorize("@yixia.hasPermi('system:dict:edit')")
    @Log(title = "字典类型--修改字典类型", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated @RequestBody SysDictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 根据id删除字典类型
     */
    @DeleteMapping("/delete/{dictIds}")
    @PreAuthorize("@yixia.hasPermi('system:dict:remove')")
    @Log(title = "字典类型--删除字典类型", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return success();
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@yixia.hasPermi('system:dict:remove')")
    @Log(title = "字典类型---刷新字典缓存", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        dictTypeService.resetDictCache();
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return success(dictTypes);
    }
}
